package com.etone.smartAudit.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.domain.Task;
import com.etone.smartAudit.domain.TaskHistory;
import com.etone.smartAudit.mapper.TaskHistoryMapper;
import com.etone.smartAudit.mapstruct.TaskMapstruct;
import com.etone.smartAudit.vo.task.TaskHistorySimpleVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-07-06 08:59:52
 * @Description 任务记录
 */
@Service
@AllArgsConstructor
public class TaskHistoryService {
    private TaskHistoryMapper taskHistoryMapper;

    private TaskMapstruct taskMapstruct;

    /**
     * 新增历史记录
     *
     * @param task
     * @param admin
     */
    public void insert(Task task, Admin admin) {
        TaskHistory taskHistory = taskMapstruct.getTaskHistory(task);
        taskHistory.setId(null);
        taskHistory.setTaskId(task.getId());
        taskHistory.setHistoryTime(new Date());
        taskHistory.setHistoryAdminId(admin.getId());
        taskHistory.setHistoryAdminName(admin.getNickName());
        save(null, taskHistory);
    }

    /**
     * 新增｜编辑
     *
     * @param id
     * @param taskHistory
     */
    public void save(Integer id, TaskHistory taskHistory) {
        if (id == null) {
            taskHistoryMapper.insert(taskHistory);
        } else {
            taskHistoryMapper.updateById(taskHistory);
        }

    }

    /**
     * 根据任务id删除相应的历史任务记录
     *
     * @param taskId
     */
    public void deleteByTaskId(Integer taskId) {
        QueryWrapper<TaskHistory> taskHistoryQueryWrapper = new QueryWrapper<>(TaskHistory.builder().taskId(taskId).build());
        taskHistoryMapper.delete(taskHistoryQueryWrapper);
    }

    /**
     * 获取编辑记录
     *
     * @param taskId
     * @return
     */
    public List<TaskHistorySimpleVO> listHistorySimple(Integer taskId) {
        return taskHistoryMapper.findListHistorySimple(taskId);
    }
}
